(function(){
	'use strict';

    var app = angular.module('myApp.login-module');
	
    app.factory( 'AuthService', [ '$http', '$window', 'SessionService', function( $http, $window, SessionService ){
    	var authService = {};
    	
    	authService.login = function( credentials ){ 
    		return $http( { method: 'GET', url: '/site/login', params: credentials } );
    	};
        	
    	authService.isAuthenticated = function() {
    	    return SessionService.user.idUser;
    	};
        	
    	authService.isAuthorized = function( authorizedRoles ){
    	    if ( !angular.isArray( authorizedRoles ) )
    	    	authorizedRoles = [ authorizedRoles ];
    	    
    	    return ( authorizedRoles.indexOf( SessionService.userRole ) !== -1 );
    	};
    	
    	authService.logout = function(){
    		SessionService.destroy();
    	    $window.location = "/site/logout";
    	};
    	
    	return authService;
	} ] );
    
})();